package cn.student.controller;

import cn.student.config.web.ResponseMessage;
import cn.student.config.web.ResponseStatusCode;
import cn.student.pojo.Work;
import cn.student.pojo.vo.JwtUser;
import cn.student.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 毛苏宇
 * User: me
 * Date: 2020/6/26
 * Time: 17:25
 * Description:
 */
@RestController
@RequestMapping("/work")
public class WorkController {

    @Autowired
    private WorkService workService;

    @PostMapping("/save")
    public ResponseMessage save(JwtUser user, Work work) {
        work.setUserId(user.getJwtId());
        boolean add = workService.add(work);
        if (add) {
            return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "作业提交成功", null);
        }
        return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "作业提交失败", null);
    }

    @PostMapping("/delete")
    public ResponseMessage delete(@RequestParam("workId") long workId) {
        boolean remove = workService.remove(workId);
        if (remove) {
            return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "删除成功", null);
        }
        return new ResponseMessage(ResponseStatusCode.CODE_FAIL, "删除失败", null);
    }

    @PostMapping("/list")
    public ResponseMessage list(@RequestParam("page") int page,
                                @RequestParam("rows") int rows) {
        return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "查询成功", workService.find(page, rows));
    }

    @PostMapping("/getWork")
    public ResponseMessage getWork(@RequestParam("workId") long workId) {
        return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "查询成功", workService.findById(workId));
    }

    @PostMapping("/update")
    public ResponseMessage update(Work work) {
        final boolean modify = workService.modify(work);
        if (modify) {
            return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "修改成功", null);
        }
        return new ResponseMessage(ResponseStatusCode.CODE_FAIL, "修改失败", null);
    }

    @PostMapping("/getWorkById")
    public ResponseMessage getWorkById(@RequestParam("userId") long userId,
                                       @RequestParam("cpId") long cpId) {
        return new ResponseMessage(ResponseStatusCode.CODE_SUCCESS, "查询成功", workService.findByUserId(userId, cpId));
    }

}
